// pages/customizationAll/customizationAll.js
import { areaList } from "../../components/area-data/dist/index.cjs";
import request from "../../utils/request";
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    item: [],
    areaList,
    result: [],
    result1: [],
    submitObj: {},
    disabledObj1: {},
    disabledObj2: {},
    radio: null,
    addId: undefined,
    id: "",
    name: "",
    phone: "",
    proxyarea: "",
    area: "",
    showarea: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList();
  },
  async getList() {
    const res = await request({
      url:
        app.globalData.baseURL +
        `/app-api/package/question-table/getQuestionTreeNew?resType=${2}`,
    });
    const list = res.data;
    const listdata = list.filter((item) => {
      return item.fieldName.includes("酒店");
    });
    if (this.data.area === "") {
      this.setData({
        area: listdata[0].childrens[0].areaId,
      });
    }
    if (
      listdata[0].childrens[0].areaId !== null &&
      listdata[0].childrens[0].contacts !== null &&
      listdata[0].childrens[0].mobile !== null
    ) {
      const areaid = listdata[0].childrens[0].areaId.toString();
      const area = Object.entries(areaList.county_list).find(
        ([key, value]) => key === areaid
      );
      this.setData({
        item: listdata[0].childrens,
        result: listdata[0].childrens,
        name: listdata[0].childrens[0].contacts,
        phone: listdata[0].childrens[0].mobile,
        proxyarea: area[1],
      });
    } else {
      this.setData({
        item: listdata[0].childrens,
        result: listdata[0].childrens,
      });
    }
    this.updateItems();
  },
  updateItems() {
    const items = this.data.item;
    const itemList = items.filter((item) => {
      if (item.fieldName.includes("备注其它个性化要求")) {
        item.childrens = [
          {
            fieldName: "个性化要求",
            fieldType: "0",
            id: item.id,
            fieldValue: item.fieldValue,
          },
        ];
      }
      item.childrens.filter((cos) => {
        const key = cos.id
        if (cos.fieldType === "2") {
          cos.fieldDesc = cos.fieldDesc.split(",");
          if (cos.fieldValue == cos.fieldDesc[0]) {
            this.setData({
              disabledObj2: { ...this.data.disabledObj2, [key]: true },
            });
          }
          if (cos.fieldValue == cos.fieldDesc[1]) {
            this.setData({
              disabledObj1: { ...this.data.disabledObj1, [key]: true },
            });
          }
        }
        if (cos.fieldValue !== null && cos.fieldType == 1) {
          this.setData({
            submitObj: { ...this.data.submitObj, [cos.id]: 1 },
          });
        }
        if (this.data.id === "") {
          this.setData({
            id: cos.resultId,
          });
        }
        if (cos.fieldValue !== null && cos.fieldType == 0) {
          this.setData({
            submitObj: { ...this.data.submitObj, [cos.id]: cos.fieldValue },
          });
        }
      });
      return item;
    });
    this.setData({
      items: itemList,
    });
  },
  goback() {
    wx.navigateBack({
      delta: 1,
    });
  },
  onChange(event) {
    const checked = event.detail.value[0];
    const key = event.currentTarget.dataset.item.id;
    if (this.data.submitObj.hasOwnProperty(key)) {
      this.setData({
        radio: checked,
        [`submitObj.${key}`]: checked,
      });
      this.firstDisabledMethod(event);
      this.lastDisabledMethod(event);
    } else {
      this.setData({
        radio: checked,
        submitObj: { ...this.data.submitObj, [key]: checked },
      });
      this.firstDisabledMethod(event);
      this.lastDisabledMethod(event);
    }
  },
  Change(event) {
    const check = event.currentTarget.dataset.item.fieldDesc;
    const key = event.currentTarget.dataset.item.id;
    if (this.data.submitObj.hasOwnProperty(key)) {
      // 如果存在，删除该键值对
      delete this.data.submitObj[key];
      this.setData({
        result: check,
      });
    } else {
      this.setData({
        result: check,
        submitObj: { ...this.data.submitObj, [key]: 1 },
      });
    }
  },
  subjctInput(event) {
    const key = event.currentTarget.dataset.item.id;
    const value = event.detail.value;
    if (value !== "") {
      this.setData({
        submitObj: { ...this.data.submitObj, [key]: value },
      });
    }
  },
  async submit() {
    if (this.data.id === "") {
      const userId = wx.getStorageSync("userId");
      const res = await request({
        url: app.globalData.baseURL + "/app-api/package/user-result/create",
        method: "POST",
        data: {
          userId: userId,
          resType: 2,
          result: JSON.stringify(this.data.submitObj),
          contacts: this.data.name,
          mobile: this.data.phone,
          areaId: this.data.area,
        },
      });
      if (res.code === 0) {
        wx.showToast({
          title: "提交成功",
          icon: "success",
        });
        setTimeout(() => {
          this.goback();
        }, 2000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
        });
      }
    } else {
      const userId = wx.getStorageSync("userId");
      const res = await request({
        url: app.globalData.baseURL + "/app-api/package/user-result/create",
        method: "POST",
        data: {
          userId: userId,
          id: this.data.id,
          resType: 2,
          result: JSON.stringify(this.data.submitObj),
          contacts: this.data.name,
          mobile: this.data.phone,
          areaId: this.data.area,
        },
      });
      if (res.code === 0) {
        wx.showToast({
          title: "提交成功",
          icon: "success",
        });
        setTimeout(() => {
          this.goback();
        }, 2000);
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
        });
      }
    }
  },
  async reset() {
    this.setData({
      submitObj: {},
    });
    const userId = wx.getStorageSync("userId");
    const res = await request({
      url: app.globalData.baseURL + "/app-api/package/user-result/create",
      method: "POST",
      data: {
        userId: userId,
        id: this.data.id,
        resType: 2,
        result: JSON.stringify(this.data.submitObj),
      },
    });
    if (res.code === 0) {
      wx.showToast({
        title: "重置成功",
        icon: "success",
      });
      this.getList();
    } else {
      wx.showToast({
        title: res.msg,
        icon: "none",
      });
    }
  },
  areabind() {
    this.setData({
      showarea: true,
    });
  },
  cancel() {
    this.setData({
      showarea: false,
    });
  },
  confirm(e) {
    this.setData({
      area: e.detail.values[2].code,
      proxyarea: e.detail.values[2].name,
    });
    this.setData({
      showarea: false,
    });
  },
  inputName(event) {
    this.setData({
      name: event.detail.value,
    });
  },
  inputPhone(event) {
    this.setData({
      phone: event.detail.value,
    });
  },
  firstDisabledMethod(cos) {
    const key = cos.currentTarget.dataset.item.id;
    if (this.data.submitObj[key] == undefined) {
      this.setData({
        [`disabledObj1.${key}`]: false,
      });
    }
    if (
      this.data.submitObj[key] == cos.currentTarget.dataset.item.fieldDesc[0]
    ) {
      this.setData({
        disabledObj2: { ...this.data.disabledObj2, [key]: true },
      });
    }
  },
  lastDisabledMethod(cos) {
    const key = cos.currentTarget.dataset.item.id;
    if (this.data.submitObj[key] === undefined) {
      this.setData({
        [`disabledObj2.${key}`]: false,
      });
    }
    if (
      this.data.submitObj[key] == cos.currentTarget.dataset.item.fieldDesc[1]
    ) {
      this.setData({
        disabledObj1: { ...this.data.disabledObj1, [key]: true },
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
